<?
/***************************************************************************
*                            Dolphin Smart Community Builder
*                              -------------------
*     begin                : Mon Mar 23 2006
*     copyright            : (C) 2007 BoonEx Group
*     website              : http://www.boonex.com
* This file is part of Dolphin - Smart Community Builder
*
* Dolphin is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the
* License, or  any later version.
*
* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Dolphin,
* see license.txt file; if not, write to marketing@boonex.com
***************************************************************************/

bx_import('BxDolTwigTemplate');

/*
 * Groups module View
 */
class BxGroupsTemplate extends BxDolTwigTemplate {

    var $_iPageIndex = 500;      
    
	/**
	 * Constructor
	 */
	function BxGroupsTemplate(&$oConfig, &$oDb) {
        parent::BxDolTwigTemplate($oConfig, $oDb);
    }

    function unitOLD ($aData, $sTemplateName, &$oVotingView) {

        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxGroupsModule');

        if (!$this->_oMain->isAllowedView ($aData)) {            
            $aVars = array ('extra_css_class' => 'bx_groups_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'), 
            'group_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aData['uri'],
            'group_title' => $aData['title'],
            'created' => defineTimeInterval($aData['created']),
            'author' => $aData['NickName'],
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'fans_count' => $aData['fans_count'],
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aData['country']]['LKey']) . (trim($aData['city']) ? ', '.$aData['city'] : ''),
        );        

        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        return $this->parseHtmlByName($sTemplateName, $aVars);
    }

    // ======================= ppage compose block functions 

    function blockDesc (&$aDataEntry) {
        $aVars = array (
            'description' => $aDataEntry['desc'],
        );
        return $this->parseHtmlByName('block_description', $aVars);
    }

    function blockFields (&$aDataEntry) {
        $sRet = '<table class="bx_groups_fields">';
        bx_groups_import ('FormAdd');        
        $oForm = new BxGroupsFormAdd ($GLOBALS['oBxGroupsModule'], $_COOKIE['memberID']);
        foreach ($oForm->aInputs as $k => $a) {
            if (!isset($a['display']) || !$aDataEntry[$k]) continue;
            $sRet .= '<tr><td class="bx_groups_field_name" valign="top">' . $a['caption'] . '<td><td class="bx_groups_field_value">';
            if (is_string($a['display']) && is_callable(array($this, $a['display'])))
                $sRet .= call_user_func_array(array($this, $a['display']), array($aDataEntry[$k]));
            else if (0 == strcasecmp($k, 'country'))
                $sRet .= _t($GLOBALS['aPreValues']['Country'][$aDataEntry[$k]]['LKey']);
            else
                $sRet .= $aDataEntry[$k];
            $sRet .= '<td></tr>';
        }
        $sRet .= '</table>';
        return $sRet;
    }

    //[begin] - ultimate groups mod from modzzz 
    function unit ($aData, $sTemplateName, &$oVotingView) {

       $bExtended = (getParam("bx_groups_extended_view")=="on") ? true : false;

        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxGroupsModule');

        if (!$this->_oMain->isAllowedBrowse()) {            
            $aVars = array (
				'extra_css_class' => 'bx_groups_unit',  
				'bx_if:compact' => array( 
					'condition' =>  (!$bExtended),
					'content' => array(),  
				),  
				'bx_if:extended' => array( 
					'condition' =>  ($bExtended),
					'content' => array(),   
				)   
			);

            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            ////$aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'thumb'), 'Search');

            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 
  
		$sDesc = $aData['desc'];
		$iLimitChars = (int)getParam('bx_groups_max_preview');
		if (strlen($sDesc) > $iLimitChars) {
			$sLinkMore = '...';
			$sDesc = process_line_output(mb_substr(strip_tags($sDesc), 0, $iLimitChars)) . $sLinkMore;
		}

        $sRate = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';
 
        $aVars = array (            
 
			'bx_if:compact' => array( 
				'condition' =>  (!$bExtended),
				'content' => array(  
					'id' => $aData['id'],
					'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'), 
					'group_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aData['uri'],
					'group_title' => $aData['title'],
					'created' => defineTimeInterval($aData['created']),
					'author' => $aData['NickName'],
					'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
					'fans_count' => $aData['fans_count'],
					'views_count' => $aData['views'],  
					'country_city' => _t($GLOBALS['aPreValues']['Country'][$aData['country']]['LKey']) . (trim($aData['city']) ? ', '.$aData['city'] : ''), 
					'rate' => $sRate,  

				),  		
			),  
			'bx_if:extended' => array( 
				'condition' =>  ($bExtended),
				'content' => array(  
					'id' => $aData['id'],
					'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'), 
					'group_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aData['uri'],
					'group_title' => $aData['title'],
					'created' => defineTimeInterval($aData['created']),
					'author' => $aData['NickName'],
					'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
					'fans_count' => $aData['fans_count'],
					'views_count' => $aData['views'],  
					'country_city' => _t($GLOBALS['aPreValues']['Country'][$aData['country']]['LKey']) . (trim($aData['city']) ? ', '.$aData['city'] : ''),
 					'rate' => $sRate,  

					'comments_count' => $aData['comments_count'],
					'created' => $this->filterDate ($aData['created']),
					'extend_created' => defineTimeInterval($aData['created']),
					 'all_categories' => $this->_oMain->parseCategories($aData['categories']),  
					'post_tags' => $this->_oMain->parseTags($aData['tags']), 		
					'group_description' => $sDesc,
					'flag_image' => genFlag($aData['country']), 
				),  		
			),  
		
		);        
 
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 
    function blockCustomFields (&$aDataEntry, $aShow=array()) {
        $sRet = '<table class="bx_groups_fields">';
        bx_groups_import ('FormAdd');        
        $oForm = new BxGroupsFormAdd ($GLOBALS['oBxGroupsModule'], $_COOKIE['memberID']);
        foreach ($oForm->aInputs as $k => $a) {
            //if (!isset($a['display'])) continue;
 
            if (!in_array($a['name'],$aShow)) continue;
            
	    if (!trim($aDataEntry[$k])) continue;

            $sRet .= '<tr><td class="bx_groups_field_name" valign="top">'. $a['caption'] . '<td><td class="bx_groups_field_value">';
            if (is_string($a['display']) && is_callable(array($this, $a['display']))){
                $sRet .= call_user_func_array(array($this, $a['display']), array($a['listname'],$aDataEntry[$k]));
	    }else{
		if($a['name'] == 'SponsorWebsite')
			$sRet .= "<a target=_blank href='".((substr($aDataEntry[$k],0,3)=="www") ? "http://".$aDataEntry[$k] : $aDataEntry[$k])."'>".$aDataEntry[$k]."</a>";
		else
			$sRet .= $aDataEntry[$k];
	    }
            $sRet .= '<td></tr>';
        }
        $sRet .= '</table>';
        return $sRet;
    }

    function filterDate ($i) {
        return getLocaleDate($i, BX_DOL_LOCALE_DATE) . ' ('.defineTimeInterval($i) . ')';
    } 
    //[end] - ultimate groups mod from modzzz 
 

    function event_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxGroupsModule');
 
		$aEventEntry = $this->_oDb->getEventEntryById($aData['id']);
		$iEntryId = $aEventEntry['group_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'bx_groups_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
 

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'event_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'event/view/' . $aData['uri'],
            'event_title' => $aData['title'],
            'event_start' => defineTimeInterval($aData['event_start']),
            'author' => $aData['author_id'] ? getNickName($aData['author_id']) : _t('_bx_groups_admin'),
            'author_url' => $aData['author_id'] ? getProfileLink($aData['author_id']) : 'javascript:void(0);',
            'author_title' => _t('_From'),
            'spacer' => getTemplateIcon('spacer.gif'),
            'participants' => $aData['fans_count'],
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aData['country']]['LKey']) . (trim($aData['city']) ? ', '.$aData['city'] : ''),
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 

    function venue_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxGroupsModule');
 
		$aEntry = $this->_oDb->getVenueEntryById($aData['id']);
		$iEntryId = $aEntry['group_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'bx_groups_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
 

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'venue/view/' . $aData['uri'],
            'title' => $aData['title'],
            'spacer' => getTemplateIcon('spacer.gif') 
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }

   function news_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxGroupsModule');
 
		$aEntry = $this->_oDb->getNewsEntryById($aData['id']);
		$iEntryId = $aEntry['group_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'bx_groups_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
 

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aAuthor = getProfileInfo($aData['author_id']); 
		$sOwnerThumb = $GLOBALS['oFunctions']->getMemberIcon($aAuthor['ID'], 'left'); 

		$sDateTime = defineTimeInterval($aData['created']);
     
		$sPostText = $aData['desc'];
		$iLimitChars = (int)getParam('bx_groups_max_preview');
		if (strlen($sPostText) > $iLimitChars) {
 			$sLinkMore = '...';
			$sPostText = process_line_output(mb_substr(strip_tags($sPostText), 0, $iLimitChars)) . $sLinkMore;
		}

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'news/view/' . $aData['uri'],
            'title' => $aData['title'],
            'description' => $sPostText,
            'post_date' => strtolower($sDateTime), 
            'post_uthumb' => $sOwnerThumb,  
             'spacer' => getTemplateIcon('spacer.gif') 
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
  
    function sponsor_unit ($aData, $sTemplateName, &$oVotingView) {
 
        if (null == $this->_oMain)
            $this->_oMain = BxDolModule::getInstance('BxGroupsModule');
 
		$aEntry = $this->_oDb->getSponsorEntryById($aData['id']);
		$iEntryId = $aEntry['group_id'];
 
        $aDataEntry = $this->_oDb->getEntryById($iEntryId);

        if (!$this->_oMain->isAllowedView ($aDataEntry)) {            
            $aVars = array ('extra_css_class' => 'bx_groups_unit');
            return $this->parseHtmlByName('browse_unit_private', $aVars);
        }
 

        $sImage = '';
        if ($aData['thumb']) {
            $a = array ('ID' => $aData['author_id'], 'Avatar' => $aData['thumb']);
            $aImage = BxDolService::call('photos', 'get_image', array($a, 'browse'), 'Search');
            $sImage = $aImage['no_image'] ? '' : $aImage['file'];
        } 

        $aVars = array (            
            'id' => $aData['id'],
            'thumb_url' => $sImage ? $sImage : $this->getIconUrl('no-photo.png'),
            'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'sponsor/view/' . $aData['uri'],
            'title' => $aData['title'],
            'spacer' => getTemplateIcon('spacer.gif') 
        ); 
 
        $aVars['rate'] = $oVotingView ? $oVotingView->getJustVotingElement(0, $aData['id'], $aData['rate']) : '&#160;';

        //$aVars = array_merge ($aVars, $aData);
        return $this->parseHtmlByName($sTemplateName, $aVars);
    }
 

    function blockSubProfileInfo ($sType, &$aData) {

		$this->_oMain = BxDolModule::getInstance('BxGroupsModule');

        $aAuthor = getProfileInfo($aData['author_id']);
 
        $aVars = array (
            'author_thumb' => get_member_thumbnail($aAuthor['ID'], 'none'),
            'date' => getLocaleDate($aData['created'], BX_DOL_LOCALE_DATE_SHORT),
            'date_ago' => defineTimeInterval($aData['created']),
            'cats' => $this->parseSubCategories($sType, $aData['categories']),
            'tags' => $this->parseSubTags($aData['tags']),
            'country_city' => _t($GLOBALS['aPreValues']['Country'][$aData['country']]['LKey']) . (trim($aData['city']) ? ', '.$aData['city'] : ''),
            'flag_image' => genFlag($aData['country']),
            'fields' => $this->blockEventFields($aData),
            'author_username' => $aAuthor ? $aAuthor['NickName'] : _t('_bx_groups_admin'),
            'author_url' => $aAuthor ? getProfileLink($aAuthor['ID']) : 'javascript:void(0)',
        );
        return $this->parseHtmlByName('block_subprofile_info', $aVars);
    }


    function blockEventFields (&$aDataEntry) {
        
		$bHasValues = false;

		$sRet = '<table class="bx_groups_fields">';
        bx_groups_import ('EventFormAdd');        
        $oForm = new BxGroupsEventFormAdd ($GLOBALS['oBxGroupsModule'], $_COOKIE['memberID']);
        foreach ($oForm->aInputs as $k => $a) {
            if (!isset($a['display'])) continue;
 
            $sRet .= '<tr><td class="bx_groups_field_name" valign="top">'. $a['caption'] . '<td><td class="bx_groups_field_value">';
            if (is_string($a['display']) && is_callable(array($this, $a['display'])))
                $sRet .= call_user_func_array(array($this, $a['display']), array($aDataEntry[$k]));
            else
                $sRet .= $aDataEntry[$k];
            $sRet .= '<td></tr>';

			$bHasValues = true; 
        }

		if(!$bHasValues)
			return;

        $sRet .= '</table>';

        return $sRet;
    }
  
    function parseSubCategories ($sType, $sVal='') {
		
		$sName = 'Group'.ucwords($sType).'Categories';

		$sStr = '';
		$aVals = explode(';',$sVal);
		foreach($aVals as $aEachVal){
			$sStr .= htmlspecialchars_adv( _t($GLOBALS['aPreValues'][$sName][$aEachVal]['LKey'])) . '&#160';
 		}
 		return $sStr;
 	}
 
    function parseSubTags($s){ 
        $sRet = '';
        $a = explode (',', $s);
         
        foreach ($a as $sName)
            $sRet .= $sName.'&#160';
  
        return $sRet;
    }

 
}

?>